import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.ruoyi.RuoYiApplication;
import com.ruoyi.address.domain.AreaEnum;
import com.ruoyi.address.domain.AreaTree;
import com.ruoyi.address.domain.ChinaArea;
import com.ruoyi.address.service.IChinaAreaService;
import com.ruoyi.address.service.impl.AddressParse;
import com.ruoyi.address.service.impl.TreeUtils;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.report.service.IHeatSourceService;
import com.ruoyi.system.domain.vo.MessageSubscribeVo;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.IWeChatService;
import org.junit.Test;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 *
 * @author lisongtao
 * @date 2023/5/11 11:14
 */
@RunWith(SpringRunner.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestInstance(value = TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = RuoYiApplication.class)
public class ValidationUtilTestServiceImpl {


    @Autowired
    private IWeChatService weChatService;

    @Autowired
    private IChinaAreaService chinaAreaService;

    @Autowired
    private IHeatSourceService heatSourceService;


    @Test
    public void test() {

    }

    @Test
    public void testGetAccessToken() {
        String accessToken = heatSourceService.getAccessToken();
        System.out.println(accessToken);
    }


    @Test
    public void testReportHeatingData() {
        heatSourceService.reportHeatingData();
    }

    @Test
    public void testDynamicDataSource() {
        String s = chinaAreaService.selectByParam();
        System.out.println(s);
        ChinaArea chinaArea = chinaAreaService.selectChinaAreaByAreaCode("110000000000");
        System.out.println(chinaArea);
        ChinaArea chinaArea1 = chinaAreaService.selectChinaAreaByAreaCode2("110105000000");
        System.out.println(chinaArea1);
    }

    @Test
    public void testPushMsg() {
        MessageSubscribeVo messageSubscribeVo = new MessageSubscribeVo();
        messageSubscribeVo.setTouser("o3NBS6XIV_pRu6nBi3-X9gZrPHJQ");
        String pagepath = "/pages_home/installationRecord/index?id=1679400509565390848";
        messageSubscribeVo.setPhrase2("何静静");
        messageSubscribeVo.setThing7("陈辉");
        messageSubscribeVo.setPhone_number8("13868129741");
        messageSubscribeVo.setThing14("浙江省-杭州市-桐庐县");
//        笪良才	13691070478	140000000000	山西省	141100000000	吕梁市	141102000000	离石区
//        1679400509565390848	130	何静静	13938664581	笪良才	13691070478	140000000000	山西省	141100000000	吕梁市	141102000000	离石区
//        1679401066589933568	130	何静静	13938664581	俞军	13301812705	340000000000	安徽省	340100000000	合肥市	340111000000	包河区
//        1679402338118356992	130	何静静	13938664581	后永东	15894150000	650000000000	新疆维吾尔自治区	654000000000	伊犁哈萨克自治州	654002000000	伊宁市
//        1679700089397923840	130	何静静	13938664581	陈辉	13868129741	330000000000	浙江省	330100000000	杭州市	330122000000	桐庐县
        messageSubscribeVo.setTime4(DateUtils.dateTime2(new Date()));
        weChatService.pushWeChatMsg(messageSubscribeVo, pagepath);
    }

    private static final List<String> arrays = Arrays.asList("QJ02	1218*198*12",
            "QJ03	1218*198*12",
            "ASXC2.7月若鎏金	1218*198*12",
            "HT2S	1218*198*12",
            "HT35防潮板	1218*198*12",
            "HTX351防潮板	1218*198*12",
            "YJ03	1218*198*12",
            "LY-01丽怡	1218*198*12",
            "BYLSW白玉兰商务	810*150*8",
            "BYLSW白玉兰商务	810*150*12",
            "锦江都城 经典版	608*105*12",
            "锦江都城 探索版	608*105*12",
            "锦江之星5.0	1218*198*8",
            "锦江之星5.0	1218*198*12",
            "锦江之星5.5	1218*198*8",
            "锦江之星5.5	1218*198*12",
            "KP-01 康铂	1218*198*12",
            "LP-02 丽柏	1215*198*12",
            "丽枫3.5 	1218*198*12",
            "WYN13 维也纳5.0	1220*401*12",
            "维也纳国际5.0	1218*198*12",
            "维也纳3好	1218*198*12",
            "七天3.0水泥灰	1218*198*8",
            "七天3.0水泥灰	1218*198*12",
            "竹纹工字扣	2.7米/根",
            "玛卡棕工字扣	2.7米/根",
            "不锈钢U字扣	3.05米/根",
            "铝合金橡木色 平扣	2.7米/根",
            "铝合金黄橡木色 平扣	2.7米/根",
            "柚木色铝合金平扣	2.7米/根",
            "门压条 铝合金搭配地板色	2.7米/根",
            "砂黄金工字扣（表面1cm宽度）	2.7米/根",
            "门压条 铝合金	2.7米/根",
            "铝合金玫瑰金 平扣	2.7米/根",
            "晶瓷灰平扣	2.7米/根",
            "晶瓷灰小直角	2.7米/根");

    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Test
    public void testInsertData() {
        Long sort = 1L;
        for (String array : arrays) {
            String[] split = array.split("\t");
            System.out.println(split[0] + "--------------" + split[1]);
            SysDictData dictData = new SysDictData();
            dictData.setDictSort(sort);
            dictData.setDictValue("value" + sort);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("型号", split[0]);
            jsonObject.put("规格", split[1]);
            dictData.setDictLabel(jsonObject.toJSONString());
            dictData.setDictType("floor_type_type");
            dictDataMapper.insertDictData(dictData);
            sort++;
        }
    }


    @Test
    public void testValidationUtil() {

    }


    public static List<AreaTree> PROVINCE_LIST, CITY_LIST, AREA_LIST;

    @Test
    public void testAddressParse() {
        Stopwatch stopwatch = Stopwatch.createStarted();
        File file1 = new File("D:\\china-area.json");
        List<String> lines = FileUtil.readUtf8Lines(file1);
        String file = String.join("", lines);

        Gson gson = new Gson();
        List<AreaTree> areas = gson.fromJson(file, new TypeToken<List<AreaTree>>() {
        }.getType());
        Iterator<AreaTree> iterator = areas.iterator();
        while (iterator.hasNext()) {
            AreaTree next = iterator.next();
            if (AreaEnum.CITY.getCode().equals(next.getLevel()) || AreaEnum.DISTRICT.getCode().equals(next.getLevel())) {
                if (StringUtils.length(next.getName()) <= 2) {
                    iterator.remove();
                }
                if (StringUtils.equals(next.getName(), "市辖区")) {
                    iterator.remove();
                }
            }
        }
        areas = TreeUtils.buildPath(areas, o -> Objects.equals(o.getParentCode(), 0L));
        Map<Integer, List<AreaTree>> areaMapping = areas.stream().collect(Collectors.groupingBy(AreaTree::getLevel));

        PROVINCE_LIST = areaMapping.get(AreaEnum.PROVINCE.getCode());
        CITY_LIST = areaMapping.get(AreaEnum.CITY.getCode());
        AREA_LIST = areaMapping.get(AreaEnum.DISTRICT.getCode());


        System.out.println("地址解析器初始化耗时：" + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
    }


    @Test
    public void testArea() {
        ArrayList<String> address = Lists.newArrayList(
                "太阳鲜鲜 盐田区山海四季城F栋17A，13111111111"
        );
        Stopwatch stopwatch = Stopwatch.createStarted();
        for (String s : address) {
            System.out.println("===============" + s + "===============");
            AddressParse.parse(s).forEach(i -> System.out.println(i.format()));
        }
        System.out.println("耗时:" + stopwatch.elapsed(TimeUnit.MILLISECONDS));
    }


//    @Autowired
//    private IChinaAreaService chinaAreaService;
//
//
//    @Test
//    public void testWeChatArea(){
//        List<ChinaAreaWeChat> regions = chinaAreaService.getChinaAreaWeChats();
//        System.out.println(JSON.toJSONString(regions));
//    }


}
